/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.MessageSource;

import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import java.util.Locale;


/**
 * Jasper reports definition.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 21-may-2009 17:02:52
 */
public class JasperReportDefinition implements Serializable {
	/**
	 * Logger for this class.
	 */
	private static final Log LOG = LogFactory.getLog(JasperReportDefinition.class);

	/**
	 * The report file.
	 */
	private Resource reportFile;

	/**
	 * The filename key.
	 */
	private String fileNameKey;

	/**
	 * The format: one of HTML, PDF, XLS (See Constants)
	 */
	private String format;

	/**
	 * The disposition: one of "attachment" or "inline".
	 */
	private String disposition;

	/**
	 * The message source.
	 */
	private MessageSource messageSource;

	/**
	 * The file report path.
	 */
	private String fileReportPath;

	/**
	 * The file report directory.
	 */
	private String fileReportDirectory;

	/**
	 * Sets the message source.
	 *
	 * @param messageSource the message source.
	 */
	public void setMessageSource(final MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	/**
	 * Retrieves the report file.
	 *
	 * @return the report file.
	 */
	public Resource getReportFile() {
		return reportFile;
	}

	/**
	 * Sets the report file.
	 *
	 * @param reportFile the report file.
	 */
	public void setReportFile(final Resource reportFile) {
		this.reportFile = reportFile;

		if(reportFile != null) {
			try {
				File theFile = reportFile.getFile();

				fileReportPath = theFile.getAbsolutePath();
				fileReportDirectory = theFile.getParent();
			} catch(IOException e) {
				LOG.error("Error retrieving report file", e);
			}
		}
	}

	/**
	 * Retrieves the report file path.
	 *
	 * @return the report file path.
	 */
	public String getReportFilePath() {
		return fileReportPath;
	}

	/**
	 * Retrieves the report directory path.
	 *
	 * @return the report directory path.
	 */
	public String getReportDirectoryPath() {
		return fileReportDirectory;
	}

	/**
	 * Retrieves the filename key.
	 *
	 * @return the filename key.
	 */
	public String getFileNameKey() {
		return fileNameKey;
	}

	/**
	 * Sets the filename key.
	 *
	 * @param fileNameKey the filename key.
	 */
	public void setFileNameKey(final String fileNameKey) {
		this.fileNameKey = fileNameKey;
	}

	/**
	 * Retrieves the file name.
	 *
	 * @param locale the locale.
	 *
	 * @return the file name.
	 */
	public String getFileName(final Locale locale) {
		return messageSource.getMessage(fileNameKey, null, fileNameKey, locale);
	}

	/**
	 * Retrieves the format.
	 *
	 * @return the format.
	 */
	public String getFormat() {
		return format;
	}

	/**
	 * Sets the format.
	 *
	 * @param format the format.
	 */
	public void setFormat(final String format) {
		this.format = format;
	}

	/**
	 * Retrieves the disposition.
	 *
	 * @return the disposition.
	 */
	public String getDisposition() {
		return disposition;
	}

	/**
	 * Sets the disposition.
	 *
	 * @param disposition the disposition.
	 */
	public void setDisposition(final String disposition) {
		this.disposition = disposition;
	}
}
